﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DotNet4.Utilities;
using System.Drawing;
using System.IO;

namespace C5Client
{
    public class C5HttpHelper
    {
        public string C5HttpHelperGetResult(string pCookie, string pReferer, string pUrl)
        {
            HttpHelper httpHelper = new HttpHelper();
            HttpItem httpItem = new HttpItem();

            httpItem.Cookie = pCookie;
            httpItem.Referer = pReferer;
            httpItem.URL = pUrl;
            HttpResult httpResult = httpHelper.GetHtml(httpItem);

            return httpResult.Html;
        }

        public string C5HttpHelperPostResult(string pCookie, string pReferer, string pUrl, string pPostData)
        {
            HttpHelper httpHelper = new HttpHelper();
            HttpItem httpItem = new HttpItem();

            httpItem.Cookie = pCookie;
            httpItem.Referer = pReferer;
            httpItem.URL = pUrl;
            httpItem.Postdata = pPostData;

            HttpResult httpResult = httpHelper.GetHtml(httpItem);

            return httpResult.Html;
        }

        public Image C5HttpHelperGetImage(string pCookie, string pReferer, string pUrl)
        {
            Image image = null;

            HttpHelper httpHelper = new HttpHelper();
            HttpItem httpItem = new HttpItem();

            httpItem.ResultType = ResultType.Byte;
            httpItem.Encoding = Encoding.Default;

            httpItem.Cookie = pCookie;
            httpItem.Referer = pReferer;
            httpItem.URL = pUrl;

            HttpResult httpResult = httpHelper.GetHtml(httpItem);

            if (httpResult.StatusCode == System.Net.HttpStatusCode.OK)
            {
                image = byteArrayToImage(httpResult.ResultByte);
            }

            return image;
        }

        private Image byteArrayToImage(byte[] Bytes)
        {
            MemoryStream ms = new MemoryStream(Bytes);
            Image outputImg = Image.FromStream(ms);
            return outputImg;
        }
    }
}
